<?php
/**
 * 图标管理
 * Created by gii.
 * User: Gii
 * Date: 2022/03/18
 * Time: 16:49
 */

namespace app\admin\controller\system;

use app\admin\logic\system\IconLogic;
use app\Request;
use think\App;

class IconController extends BaseController
{
	public $title = '图标';

	public function __construct(App $app)
	{
		parent::__construct($app);
	}

	// 表格页面
	public function index() {
        $search = [
            [
                'type'  => 'input',
                'key'   => 'id',
                'name'  => '请输入ID',
            ],
            [
                'type'  => 'input',
                'key'   => 'icon',
                'name'  => '请输入图标',
            ],
        ];
        $searchHtml = makeSearch($search, []);
        $this->assign('search', $searchHtml);
		return $this->fetch();
	}

	// 获取数据
    public function getData(Request $request) {
    	$page = $request->get('page', 1);
    	$pageSize = $request->get('limit', 10);

        $search = $request->get('key', []);

    	$data = IconLogic::getListWithCount($search, '*', $page, $pageSize);
    	return $this->setSuccess($data->getList(), $data->getCount());
    }

	// 添加
	public function add(Request $request) {
		if($post = $request->post()) {
			$res = IconLogic::add($post);
			if($res) {
				return $this->setSuccess();
			} else {
				return $this->setError(\Debug::getDebug());
			}
		}

		return $this->fetch(['get'=>[]], 'form');
	}

	// 编辑
	public function update(Request $request) {
		if($post = $request->post()) {
			$res = IconLogic::update($post);
			if($res === false) {
				return $this->setError(\Debug::getDebug());
			} else {
				return $this->setSuccess();
			}
		}

		$id = $request->get('id');
		$get = IconLogic::getOne($id);

		return $this->fetch(['get'=>$get], 'form');
	}

	// 删除
	public function del(Request $request) {
		if(!$id = $request->get('id')) {
			return $this->setError('请提交要删除的数据');
		}

		$res = IconLogic::del($id);
		if(!$res) {
			return $this->setError('删除失败');
		}
		return $this->setSuccess();
	}

	// 状态
	public function status(Request $request) {
		if(!$id = $request->get('id')) {
			return $this->setError('请选择要修改的数据');
		}
		$status = $request->get('status');
		$res = IconLogic::status($id, $status);
		if(!$res) {
			return $this->setError('操作失败');
		}
		return $this->setSuccess();
	}

}